package cn.dglydrpy.study.j2ee.servlet.day12;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.dglydrpy.study.j2ee.jdbc.utils.DBUtil;

/*
 * model层
 */
public class BmService {
	
	public String bmi(double height,double weight){
		double bmi = 10000*weight/height/height;
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			String sql = "insert into bmi values(null,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setDouble(1, height);
			ps.setDouble(2, weight);
			ps.setDouble(3, bmi);
			ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				DBUtil.close(conn, ps, rs);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
		
		if (bmi>25) {
			return "体重过重";
		}else if (bmi<19) {
			return "体重过轻";
		}else{
			return "体重正常";
		}
		
	}
}
